//
//  MessageLeftTableViewCell.m
//  intelligent
//
//  Created by 甘世清 on 2024/2/7.
//

#import "MessageLeftTableViewCell.h"
#import "ScoreView.h"
#import "FankuiViewController.h"
@implementation MessageLeftTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    [self.contentView addSubview:self.avatarImageView];
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.size.mas_equalTo(52);
        make.left.equalTo(self.contentView).offset(12);
    }];
    [self.contentView addSubview:self.lottieView];
    [self.lottieView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(68);
        make.height.mas_equalTo(40);
        make.left.equalTo(self.avatarImageView.mas_right).offset(12);
        make.centerY.equalTo(self.avatarImageView);
    }];

    [self.contentView addSubview:self.yuyinBtn];
    [self.yuyinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.lottieView);
    }];
    [self.yuyinBtn addSubview:self.hud];
    
    [self.contentView addSubview:self.fuzhiBtn];
    [self.fuzhiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(34);
        make.left.equalTo(self.lottieView.mas_right).offset(12);
        make.centerY.equalTo(self.avatarImageView);
    }];
    
    [self.contentView addSubview:self.loveBtn];
    [self.loveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(34);
        make.left.equalTo(self.fuzhiBtn.mas_right);
        make.centerY.equalTo(self.avatarImageView);
    }];
    self.bgView = [[UIView alloc]init];
    self.bgView.layer.cornerRadius = 4;
    self.bgView.layer.masksToBounds = YES;
    self.bgView.backgroundColor = SQColor(@"#CDFDF2");
    [self.contentView addSubview:self.bgView];
    [self.contentView addSubview:self.messageLabel];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarImageView.mas_bottom).offset(22);
        make.left.equalTo(self.contentView).offset(18);
    }];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.messageLabel).insets(UIEdgeInsetsMake(-6, -6, -6, -2));
    }];
    [self.bgView addSubview:self.hud2];
    
  
//    self.FS_cellBottomView = self.messageLabel;
}
- (void)setModel:(MessageModel *)model{
    
    _model = model;

    if ([model.portrait isEqualToString:@"智能 头像"]) {
        self.avatarImageView.image = SQGetImage(model.portrait);
    }else{
        [self.avatarImageView sd_setImageWithURL:SQURLImage(model.portrait)];
    }
   
    if (model.isCollect) {
        self.loveBtn.image = SQGetImage(@"收藏-亮");
    } else {
        self.loveBtn.image = SQGetImage(@"收藏-灰");
    }
   
    if (model.typewriterType == 1) {
        self.model.typewriterType = 2;
       //开始打字机效果
        self.bgView.backgroundColor = SQColor(@"#CDFDF2");
        self.messageLabel.text = @"";
        [self startPrintText];
    }else if(model.typewriterType == 0) {
        self.messageLabel.text = model.content;
        if (model.type == 1) {
            self.bgView.backgroundColor = UIColor.clearColor;
            [self.hud2 startAnimating];
            [self.messageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(40);
                make.height.mas_equalTo(40);
            }];
        }else{
            self.bgView.backgroundColor = SQColor(@"#CDFDF2");
            [self.messageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(model.cellSize.width);
                make.height.mas_equalTo(model.cellSize.height);
            }];
        }
    }else{
        
    }
   
    [self.contentView layoutIfNeeded];

}

-(void)SQ_yuyinButtonClick:(UIButton*)btn{
    if (self.model.type ==1) {
        return;
    }
    if (self.model.content.length > 300) { //大于300本地播放.
        [self SQ_benDiYuyinBofang];
    } else {//小于300服务器合成
        btn.backgroundColor =  SQColor(@"#4AF8CE");
        [self.hud startAnimating];
        if ([self.model.url isNotEmpty]) {
            if (self.typewriteEffectBlock) {
                self.typewriteEffectBlock();
            }
        } else {
            NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
            [dic setObject:self.model.userid forKey:@"id"];
            [HttpTool post:@"chat/voice" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
               
                self.model.url = httpModel.data;
                if (self.typewriteEffectBlock) {
                    self.typewriteEffectBlock();
                }
            } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
                [self SQ_benDiYuyinBofang];
            }];
        }
    }
}
-(void)SQ_UISetUp{
    [self.hud stopAnimating];
    self.yuyinBtn.backgroundColor = UIColor.clearColor;
    [self.lottieView stop];
}
-(void)SQ_benDiYuyinBofang{
    if (self.model.type ==1) {
        return;
    }
    if (self.typewriteEffectBlock) {
        self.typewriteEffectBlock();
    }
}



-(void)startPrintText{
    [self.receivedStrings removeAllObjects];
    for (NSUInteger i = 0; i < [self.model.content length]; i++) {
        unichar character = [self.model.content characterAtIndex:i];
        NSString *characterString = [NSString stringWithCharacters:&character length:1];
        [self.receivedStrings addObject:characterString];
    }
    // 定义一个计数器
    __block NSInteger currentIndex = 0;
    WS(weakSelf);
    NSTimeInterval delay = 0.05; // 每个字符显示的时间间隔
    if (self.receivedStrings.count > 200) {
        delay = 0.02;
    }
    // 定义一个定时器，每隔0.03秒执行一次
    if (self.timer) {
        _timer = NULL;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:delay repeats:YES block:^(NSTimer * _Nonnull timer) {
        // 在主队列中执行更新 UI 的操作
        dispatch_async(dispatch_get_main_queue(), ^{
            // 判断是否已经拼接完所有的字符串
            if (currentIndex < weakSelf.receivedStrings.count) {
                @autoreleasepool {
                    // 获取当前需要拼接的字符串
                    NSString *stringToAppend = weakSelf.receivedStrings[currentIndex];
                    weakSelf.messageLabel.text = [NSString stringWithFormat:@"%@%@",weakSelf.messageLabel.text,stringToAppend];
                  
                }
                currentIndex++;
            } else {
                [weakSelf.timer invalidate];
                weakSelf.model.typewriterType = 0;
                if ([ScoreView SQ_ShowScoreView]) {
                    [weakSelf pushScoreView];
                }
            }
            CGSize size =  [MessageModel SQ_GetSize:weakSelf.messageLabel];
             [weakSelf.messageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                 make.width.mas_equalTo(size.width);
                 make.height.mas_equalTo(size.height);
             }];
             [weakSelf.contentView layoutIfNeeded];
        });
    }];
}

-(void)pushScoreView{
    ScoreView  * view = [[ScoreView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
   
    [view setClickCancelBtnBlock:^{
        [wk_pop dismiss];
    }];
    [view setClickConfireBtnBlock:^{
        [wk_pop dismiss];
        [SKStoreReviewController requestReview];
    }];
    [view setClickFeedbackBtnBlock:^{
        [wk_pop dismiss];
        FankuiViewController *vc = [[FankuiViewController alloc]init];
        [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
    }];
}
- (NSMutableArray *)receivedStrings{
    if (!_receivedStrings) {
        _receivedStrings = [[NSMutableArray alloc]init];
    }
    return _receivedStrings;
}

- (LOTAnimationView *)lottieView{
    if (!_lottieView) {
        _lottieView  = [LOTAnimationView animationNamed:@"data.json"];
        _lottieView.loopAnimation = YES;
        _lottieView.backgroundColor = SQColor(@"#4AF8CE");
        _lottieView.layer.cornerRadius = 4;
        _lottieView.layer.masksToBounds = YES;
    }
    return _lottieView;
}
- (UIButton *)yuyinBtn{
    if (!_yuyinBtn) {
        _yuyinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _yuyinBtn.backgroundColor = UIColor.clearColor;
        _yuyinBtn.layer.cornerRadius = 4;
        [_yuyinBtn addTarget:self action:@selector(SQ_yuyinButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _yuyinBtn;
}
    
- (UIImageView *)avatarImageView{
    if (!_avatarImageView) {
        _avatarImageView = [[UIImageView alloc]init];
        _avatarImageView.clipsToBounds = YES;
        _avatarImageView.layer.cornerRadius = 8;
        _avatarImageView.layer.masksToBounds = YES;
        _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _avatarImageView;
}

- (UILabel *)messageLabel{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = FONT_REGULAR(14);
        _messageLabel.textColor = SQColor333333;
        _messageLabel.backgroundColor = UIColor.clearColor;
        _messageLabel.numberOfLines = 0;
//        _messageLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _messageLabel;
}
- (SQCustomButton *)loveBtn{
    if (!_loveBtn) {
        _loveBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(24, 24) midmargin:0];
        _loveBtn.image = SQGetImage(@"收藏-灰");
    }
    return _loveBtn;
}
- (SQCustomButton *)fuzhiBtn{
    if (!_fuzhiBtn) {
        _fuzhiBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(24, 24) midmargin:0];
        _fuzhiBtn.image = SQGetImage(@"复制");
    }
    return _fuzhiBtn;
}

- (UIActivityIndicatorView *)hud{
    if (!_hud) {
        _hud = [[UIActivityIndicatorView  alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
        _hud.hidesWhenStopped = YES;
        _hud.center = CGPointMake(34, 20);
        _hud.color = SQColor333333;
    }
    return _hud;
}
- (UIActivityIndicatorView *)hud2{
    if (!_hud2) {
        _hud2 = [[UIActivityIndicatorView  alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
        _hud2.hidesWhenStopped = YES;
        _hud2.center = CGPointMake(10, 10);
        _hud2.color = UIColor.blackColor;
    }
    return _hud2;
}
@end
